<template>
  <div class="integral-mall">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="router.back()">
        <span class="back-icon">←</span>
      </div>
      <h1>积分商城</h1>
    </div>

    <!-- 积分余额卡片 -->
    <div class="points-card">
      <div class="points-info">
        <span class="points-value">286</span>
        <span class="points-label">可用积分</span>
      </div>
      <div class="points-rules" @click="showRules = true">积分规则</div>
    </div>

    <!-- 商品分类 -->
    <div class="category-tabs">
      <div
        v-for="(category, index) in categories"
        :key="index"
        class="tab-item"
        :class="{ active: currentCategory === index }"
        @click="currentCategory = index"
      >
        {{ category }}
      </div>
    </div>

    <!-- 商品列表 -->
    <div class="products-grid">
      <div v-for="product in products" :key="product.id" class="product-card" @click="showProductDetail(product)">
        <div class="product-image">
          <img :src="product.image" :alt="product.name">
        </div>
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="product-desc">{{ product.description }}</p>
          <div class="product-bottom">
            <span class="points-cost">{{ product.points }}积分</span>
            <button class="exchange-btn" @click.stop="showExchangeConfirm(product)">兑换</button>
          </div>
        </div>
      </div>
    </div>

    <!-- 商品详情弹窗 -->
    <div class="modal" v-if="showDetail" @click="showDetail = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>商品详情</h3>
          <span class="close-btn" @click="showDetail = false">×</span>
        </div>
        <div class="modal-body" v-if="selectedProduct">
          <img :src="selectedProduct.image" :alt="selectedProduct.name">
          <h3>{{ selectedProduct.name }}</h3>
          <p>{{ selectedProduct.description }}</p>
          <div class="points-info">
            <span class="points-label">所需积分：</span>
            <span class="points-value">{{ selectedProduct.points }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 积分规则弹窗 -->
    <div class="modal" v-if="showRules" @click="showRules = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>积分规则</h3>
          <span class="close-btn" @click="showRules = false">×</span>
        </div>
        <div class="modal-body">
          <div class="rules-content">
            <p>1. 每消费1元可获得1积分</p>
            <p>2. 会员可享受1.2倍积分加速</p>
            <p>3. 积分可用于兑换商品</p>
            <p>4. 积分有效期为一年</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 分类数据
const categories = ['全部', '饮品', '甜点', '周边', '礼品卡']
const currentCategory = ref(0)

// 商品数据
const products = [
  {
    id: 1,
    name: '芝士蛋糕',
    description: '浓郁芝士，松软可口',
    points: 299,
    image: 'https://example.com/cake.jpg'
  },
  {
    id: 2,
    name: '咖啡券',
    description: '任意精品咖啡一杯',
    points: 199,
    image: 'https://example.com/coffee.jpg'
  },
  // 更多商品...
]

// 弹窗控制
const showDetail = ref(false)
const showRules = ref(false)
const selectedProduct = ref(null)

// 显示商品详情
const showProductDetail = (product) => {
  selectedProduct.value = product
  showDetail.value = true
}

// 显示兑换确认
const showExchangeConfirm = (product) => {
  if (confirm(`确认使用${product.points}积分兑换${product.name}？`)) {
    // TODO: 处理兑换逻辑
    alert('兑换成功！')
  }
}
</script>

<style scoped>
.integral-mall {
  padding-bottom: 76px;
  background-color: #f8f9fa;
  min-height: 100vh;
  max-width: 430px;
  margin: 0 auto;
  animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #42b883 0%, #35495e 100%);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  color: white;
  height: 50px;
}

.back-icon {
  font-size: 24px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  margin-right: 16px;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}


.nav-header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  text-align: center;
}

.points-card {
  margin: 20px;
  margin-top: 90px;
  padding: 24px;
  background: linear-gradient(135deg, #42b883, #35495e);
  border-radius: 16px;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.points-info {
  display: flex;
  flex-direction: column;
}

.points-value {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 4px;
}

.points-label {
  font-size: 14px;
  opacity: 0.9;
}

.points-rules {
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
}

.category-tabs {
  display: flex;
  padding: 0 20px;
  margin-bottom: 20px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-item {
  padding: 8px 16px;
  margin-right: 12px;
  background: white;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.3s;
}

.tab-item.active {
  background: #42b883;
  color: white;
}

.products-grid {
  padding: 0 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.product-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.3s;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-4px);
}

.product-image {
  width: 100%;
  height: 150px;
  background: #f5f5f5;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 12px;
}

.product-info h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.product-desc {
  margin: 4px 0 8px;
  font-size: 12px;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.points-cost {
  color: #42b883;
  font-weight: bold;
}

.exchange-btn {
  padding: 4px 12px;
  background: #42b883;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 12px;
  cursor: pointer;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 400px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  padding: 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  font-size: 24px;
  color: #999;
  cursor: pointer;
}

.modal-body {
  padding: 16px;
}

.rules-content p {
  margin: 8px 0;
  font-size: 14px;
  color: #666;
}
</style>